Okay, so in today's lesson we are going to be learning the basics about tables.
Okay.
We're going to learn more about how they use and how to create them today.
And then in the next lesson that's when we're going to properly look at how they can be so useful.
Okay.
Um, so first off we need to know what a table actually is.
Okay.
So a table is just a list of lots of different bits of data.
Okay.
So imagine a variable.
But instead of you only assigning it to one value, you can assign it as many values as you like.
That's the power of tables.
You can make a table of your favorite foods.
You can make a table of band players.
Okay.
Um, so let's add a script into the game.
These can be done either on server scripts or local scripts.
Okay.
So first off, let's show let's show you how to actually make a table.
So you start off in a similar kind of way you would do with variables.
You write a local keyword.
You then name your worst case table not variable.
Let's call this my table here.
Okay.
You can make for equal to some stuff.
But again obviously we're not just writing one thing we're writing multiple.
Okay.
So you need to use these curly brackets here.
I'm not sure where their actual name I call them curly brackets, but I'm sure you can see them and
find them somewhere on your keyboard.
Okay.
And they are used when it comes to tables.
Okay.
And inside here you can write as many bits of data as you like with commas in between them.
So I could write some strings.
I could say item one.
Comma.
Item two.
Comma.
Item three.
Just like that.
Okay.
Or of course I could add in a color three.
I could add in a boolean.
I could add in whatever I like to this.
Okay.
But I think just for this example it might be easier to just use strings.
But yeah okay.
So we've now got this variable which stores three different things.
Okay.
Let's see what happens when we print my table.
Okay.
So let's run the game.
It prints all three.
Okay.
It prints.
Um.
It has this one is equal to, two is equal to, and three is equal to thing.
But again we'll have a look at that a bit more next lesson what that means.
But here we have item one item to item three.
So what that means is when we say my table we are basically using all everything in cyber tables.
In this case we're using our three strings.
But what if we only wanted to use one of them?
Well in that case you would write after you say the name of your table, the square brackets here,
and then the number it is in the table.
Okay.
So if we say my table afterwards in square brackets to let's see what that runs instead.
It runs item.
It prints item two.
Okay, so it's the second one in the list.
So we can use it just by saying in square brackets like that there.
Okay.
So to reference all of your or everything in your list you would just save a name of your table.
Whereas to reference just just one in your table you would save a table name.
And then in square brackets the number there is in the table.
Okay.
This is great.
Um, however, what if we wanted to add or remove values from our table?
For that we can use the table library of functions.
Okay.
Um, so we could say you reference values by you saying table.
That's the keyword for that.
And this stores a bunch of different functions to do with tables that you can use.
Okay.
So let's write Dot.
And the two we're looking at here is dot insert and remove.
Dot insert allows you to add a new value onto your table.
And it will automatically add it towards the end.
So if there's three values currently it will add number four okay.
So first you write down what table you're wanting to add this value onto as obviously we only have one
table in this case.
But you might have multiple tables in a different script.
And we're going to say my table that's what our table is called here.
Then we put the value of the next bit of data in our table.
In this case is going to be item four.
Okay.
And that's it.
That will then add um item four onto our table.
So we have a four value just here.
Okay.
It's that simple to add values onto your table to remove them.
Um there technically are two ways to remove them.
Okay.
Um, you could if you want to use table dot remove.
And what table dot remove will do is it will, um get rid of that item and shift all of the ones after
it one down.
So item one will stay.
Let's say we delete item two.
Then item three will shift down to take the place of item two.
And our item four will shift down to take place of item three.
Okay.
And you can do that by saying table dot remove.
Again in here we write down our table.
So my table and the position that we want to delete.
So I want to delete the second one here for example.
Okay this is great.
But again it shifts to more down.
If there's a case where you don't want to shift them down.
You could instead just set that value to equal nil.
Now I'm not sure if you came across nil before.
It's written like this and is a keyword actually in Roblox studio.
Um, but basically nil um, is a fancy way of saying nothing.
Okay.
Um, so nil basically means nothing, doesn't mean zero.
I know zero is is is nothing, but zero is still a number.
Nil is like nothing.
Okay.
Um, so by saying that my table brackets two.
So the second value in the table is equal to nil.
Okay.
That means we are updating it so that, um, there's still something there you can assign.
But item two itself is gone and is a placeholder nil there.
Um, and all the other items stay in the same place.
Okay.
So to remove you kind of got two options.
Whereas insert you've got this obvious option here.
Okay.
And there you go.
That's the simple um, the simple bits of creating a table.
Okay.
If you have a list of banned players, for example, so that if a player is on this list, they're not
allowed to join your game, you could start off by creating a table, or it might be a blank table in
this case.
Um, and then as you as you find out more players are cheating in your game, you can add them onto
the table using Table.insert.
Then maybe after a certain amount of time has occurred.
Or maybe you, um, a player was banned when he shouldn't have been banned.
You can remove them using table dot remove if you wanted to like this as well.
Okay, so hopefully that gives you a good understanding of tables.
Okay.
Now you might be thinking, you know, why are they useful?
Um well don't worry.
Okay.
Next lesson we're going to properly look at how tables can be so useful.
And trust me, um, they are very useful.
Okay, so, um, make sure you have a good understanding of tables and join me in the next lesson where
we're going to have a look at some of the applications of using tables.